package wangjg.codec.amr;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class PCMPlayer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			File file = new File("d:\\temp\\luyin.pcm");
			InputStream fis = new FileInputStream(file);
			
			int bufferSize = (int) file.length();
			byte[] audioData = new byte[bufferSize];
			
			fis.read(audioData);

			float sampleRate = 8000;
			int sampleSizeInBits = 16;
			int channels = 1;
			boolean signed = true;
			boolean bigEndian = false;
			// sampleRate - 每秒的样本数
			// sampleSizeInBits - 每个样本中的位数
			// channels - 声道数（单声道 1 个，立体声 2 个）
			// signed - 指示数据是有符号的，还是无符号的
			// bigEndian - 指示是否以 big-endian 字节顺序存储单个样本中的数据（false 意味着 little-endian）
			AudioFormat audioFormat = new AudioFormat(sampleRate, sampleSizeInBits,channels, signed, bigEndian);
			SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class,audioFormat, bufferSize);
			SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
			sourceDataLine.open(audioFormat);
			sourceDataLine.start();
			
			for (int offset = 0; offset < audioData.length; ) {
				offset += sourceDataLine.write(audioData, offset, bufferSize);
			}
			
			//sdl.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}